package com.android.baseconfig.common.views;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.baseconfig.R;
import com.android.baseconfig.common.utils.ViewUtils;

import static android.util.TypedValue.COMPLEX_UNIT_PX;


/**
 * app:civ_left_text
 * app:civ_right_text
 * app:civ_left_color
 * app:civ_right_color
 * app:civ_left_drawable
 * app:civ_right_drawable
 * app:civ_line
 *
 * @author lei
 */
public class CustomItemView extends RelativeLayout {

    private ImageView itemNameIcon;
    private TextView itemName;
    private TextView titleName;
    private ImageView itemIconRight;
    private TextView itemNameValue;
    // private Switch itemSwitch;
    private SlideButton itemSwitch;
    private View line;
    private View topLine;

    public ImageView getItemNameIcon() {
        return itemNameIcon;
    }

    public TextView getItemName() {
        return itemName;
    }

    public SlideButton getItemSwitch() {
        return itemSwitch;
    }

    public TextView getTitleName() {
        return titleName;
    }

    public ImageView getItemIconRight() {
        return itemIconRight;
    }

    public TextView getItemNameValue() {
        return itemNameValue;
    }

    public void setItemNameValue(String value) {
        itemNameValue.setText(value);
    }

    public void setItemNameValue(int value) {
        itemNameValue.setText(getContext().getResources().getString(value));
    }

    public void setItemIconRightGone() {
        ViewUtils.setViewVisible(itemIconRight, View.GONE);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)itemNameValue.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        itemNameValue.setPadding(itemNameValue.getPaddingLeft(), itemNameValue.getPaddingTop(),
                getContext().getResources().getDimensionPixelSize(R.dimen.dp_15), itemNameValue.getPaddingBottom());
        itemNameValue.setLayoutParams(params); //使layout更新
    }

    public void setItemName(String name) {
        itemName.setText(name);
    }

    public void setItemName(int name) {
        itemName.setText(getContext().getResources().getString(name));
    }

    public void setItemSwitch(boolean value) {
        itemSwitch.setOpen(value);
    }

    public void setLine(boolean value) {
        line.setVisibility(value ? VISIBLE : GONE);
    }

    public CustomItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.custom_item_view);
        String customItemViewLeftText = typedArray.getString(R.styleable.custom_item_view_civ_left_text);
        String customItemViewRightText = typedArray.getString(R.styleable.custom_item_view_civ_right_text);
        String customItemViewTitleText = typedArray.getString(R.styleable.custom_item_view_civ_title_text);
        int customItemViewLeftColor = typedArray.getColor(R.styleable.custom_item_view_civ_left_color, context.getResources().getColor(R
				.color.content_default));
        int customItemViewRightColor = typedArray.getColor(R.styleable.custom_item_view_civ_right_color, context.getResources().getColor
				(R.color.content_assist));
        int customItemViewTitleColor = typedArray.getColor(R.styleable.custom_item_view_civ_title_color, context.getResources().getColor
				(R.color.content_default));
        int customItemViewLeftTextSize = typedArray.getDimensionPixelSize(R.styleable.custom_item_view_civ_left_text_size, context
				.getResources().getDimensionPixelSize(R.dimen.sp_14));
        int customItemViewRightTextSize = typedArray.getDimensionPixelSize(R.styleable.custom_item_view_civ_right_text_size, context
				.getResources().getDimensionPixelSize(R.dimen.sp_14));
        int customItemViewTitleTextSize = typedArray.getDimensionPixelSize(R.styleable.custom_item_view_civ_title_text_size, context
				.getResources().getDimensionPixelSize(R.dimen.sp_14));
        int customItemViewLeftDrawable = typedArray.getResourceId(R.styleable.custom_item_view_civ_left_drawable, -1);
        int customItemViewRightDrawable = typedArray.getResourceId(R.styleable.custom_item_view_civ_right_drawable, -1);
        boolean customItemSwitch = typedArray.getBoolean(R.styleable.custom_item_view_civ_switch, false);
        int customItemViewLineColor = typedArray.getColor(R.styleable.custom_item_view_civ_line_color, context.getResources().getColor
                (R.color.white_eaeaea));
        boolean customItemViewLine = typedArray.getBoolean(R.styleable.custom_item_view_civ_line, false);
        int customItemViewLineHeight = typedArray.getDimensionPixelSize(R.styleable.custom_item_view_civ_line_height, -1);
        boolean customItemViewTopLine = typedArray.getBoolean(R.styleable.custom_item_view_civ_top_line, false);
        int customItemViewTopLineHeight = typedArray.getDimensionPixelSize(R.styleable.custom_item_view_civ_top_line_height, -1);
        int bgResource = typedArray.getResourceId(R.styleable.custom_item_view_civ_bg, R.drawable.bg_fb_white_corner);
        typedArray.recycle();
        initView();
        if (customItemViewLeftDrawable != -1) {
            ViewUtils.setViewVisible(itemNameIcon, View.VISIBLE);
            itemNameIcon.setImageResource(customItemViewLeftDrawable);
        } else {
            ViewUtils.setViewVisible(itemNameIcon, View.GONE);
        }
        if (customItemViewRightDrawable != -1) {
            ViewUtils.setViewVisible(itemIconRight, View.VISIBLE);
            ViewUtils.setViewVisible(itemSwitch, View.GONE);
            itemIconRight.setImageResource(customItemViewRightDrawable);
            // ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) itemNameValue.getLayoutParams();
            // p.setMargins(0, 0, -10, 0);
            itemNameValue.setPadding(itemNameValue.getPaddingLeft(), itemNameValue.getPaddingTop(),
                    context.getResources().getDimensionPixelSize(R.dimen.dp_6), itemNameValue.getPaddingBottom());
        } else {
            ViewUtils.setViewVisible(itemIconRight, View.GONE);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)itemNameValue.getLayoutParams();
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            itemNameValue.setLayoutParams(params); //使layout更新
            ViewUtils.setViewVisible(itemSwitch, customItemSwitch ? View.VISIBLE : GONE);
        }

        itemName.setText(customItemViewLeftText);
        itemName.setTextSize(COMPLEX_UNIT_PX, customItemViewLeftTextSize);
        itemName.setTextColor(customItemViewLeftColor);
        titleName.setText(customItemViewTitleText);
        titleName.setTextSize(COMPLEX_UNIT_PX, customItemViewTitleTextSize);
        titleName.setTextColor(customItemViewTitleColor);
        itemNameValue.setText(customItemViewRightText);
        itemNameValue.setTextSize(COMPLEX_UNIT_PX, customItemViewRightTextSize);
        itemNameValue.setTextColor(customItemViewRightColor);
        if (customItemViewLineHeight != -1) {
            line.setVisibility(VISIBLE);
            ViewGroup.LayoutParams params = line.getLayoutParams();
            params.height = customItemViewLineHeight;
            line.setLayoutParams(params);
            line.setBackgroundColor(customItemViewLineColor);
        } else {
            line.setVisibility(customItemViewLine ? View.VISIBLE : View.GONE);
        }
        if (customItemViewTopLineHeight != -1) {
            topLine.setVisibility(VISIBLE);
            ViewGroup.LayoutParams params = topLine.getLayoutParams();
            params.height = customItemViewTopLineHeight;
            topLine.setLayoutParams(params);
            topLine.setBackgroundColor(customItemViewLineColor);
        } else {
            topLine.setVisibility(customItemViewTopLine ? View.VISIBLE : View.GONE);
        }
        setBackgroundResource(bgResource);
    }

    public CustomItemView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public CustomItemView(Context context) {
        this(context, null);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void initView() {
        View.inflate(getContext(), R.layout.view_custom_item, this);
        itemNameIcon = (ImageView) findViewById(R.id.item_name_icon);
        itemName = (TextView) findViewById(R.id.item_name);
        titleName = (TextView) findViewById(R.id.title_name);
        itemIconRight = (ImageView) findViewById(R.id.item_icon_right);
        itemNameValue = (TextView) findViewById(R.id.item_name_value);
        itemSwitch = (SlideButton) findViewById(R.id.item_switch);
        line = findViewById(R.id.line);
        topLine = findViewById(R.id.top_line);
    }

}
